<?php defined('SYSPATH') or die();?>
<!doctype html>
<html>
<head>
    <meta charset="utf-8">
    <title>思途CMS{$coreVersion}</title>
    {template 'stourtravel/public/public_min_js'}
    {php echo Common::getCss('style.css,base.css,base2.css,base_new.css,logistics.css'); }
</head>
<body>

<table class="content-tab">
    <tr>
        <td width="119px" class="content-lt-td" valign="top">
            {template 'stourtravel/public/leftnav'}
            <!--右侧内容区-->
        </td>
        <td valign="top" class="content-rt-td" style="overflow-x:hidden;">
            <div class="cfg-header-bar">
                <a href="javascript:;" class="fr btn btn-primary radius mt-6 mr-10" onclick="window.location.reload()">刷新</a>
            </div>
            <div class="clearfix">
                <form id="st_frm">
                <div class="logistics-container">
                    <ul class="info-item-block">
                        <li>
                            <span class="item-hd">配送方式名称：</span>
                            <div class="item-bd">
                                <input class="input-text w300" type="text" name="title" value="{$info['title']}" placeholder="配送方式" />
                            </div>
                        </li>
                        <li>
                            <span class="item-hd">计价方式：</span>
                            <div class="item-bd">
                                <label class="radio-label"><input value="1" type="radio" {if $info['type']<2} checked {/if} name="type" />按重量</label>
                                <label class="radio-label ml-20"><input value="2" type="radio" name="type"  {if $info['type']==2} checked {/if} />按件数</label>
                                <label class="radio-label ml-20"><input  value="3" type="radio" name="type"  {if $info['type']==3} checked {/if} />按体积</label>
                            </div>
                        </li>
                        <li>
                            <span class="item-hd">配送区域设置：</span>
                            <div class="item-bd">
                                <table class="table table-border table-bordered mb-10">
                                    <thead>
                                    <tr class="text-c">
                                        <th width="50%" class="text-l">可配送至</th>

                                        <th width="10%" id="type_start">
                                            {if $info['type']<2}
                                            首重（g）
                                            {elseif $info['type']==2}
                                            首件（件）
                                            {else}
                                            首件（立方米）
                                            {/if}
                                        </th>

                                        <th width="10%">运费（元）</th>
                                        <th width="10%" id="type_continue">
                                            {if $info['type']<2}
                                            续重（g）
                                            {elseif $info['type']==2}
                                            续件（件）
                                            {else}
                                            续件（立方米）
                                            {/if}
                                        </th>
                                        <th width="10%">续费（元）</th>
                                        <th width="10%">管理</th>
                                    </tr>
                                    </thead>
                                    <tbody id="planlist">
                                    {loop $info['planlist'] $plan}
                                    <tr>
                                        <td>
                                            <span>{$plan['city_titlelist']}</span>
                                            <br><a class="btn-link edit-plan"  data-id="{$plan['id']}" href="javascript:;">编辑</a>
                                            <input class="provincelist"  type="hidden" value="{$plan['provincelist']}" name="provincelist[{$plan['id']}]">
                                            <input class="citylist"  type="hidden" value="{$plan['citylist']}" name="citylist[{$plan['id']}]">
                                        </td>
                                        <td><input type="number" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^0-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
                                                   onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^0-9]/g,'0')}else{this.value=this.value.replace(/\D/g,'')}"  class="input-text start_num" name="start_num[{$plan['id']}]" value="{$plan['start_num']}" /></td>
                                        <td><input type="number" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^0-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
                                                   onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^0-9]/g,'0')}else{this.value=this.value.replace(/\D/g,'')}" class="input-text start_price" name="start_price[{$plan['id']}]" value="{$plan['start_price']}" /></td>
                                        <td><input type="number" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^0-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
                                                   onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^0-9]/g,'0')}else{this.value=this.value.replace(/\D/g,'')}" class="input-text continue_num" name="continue_num[{$plan['id']}]" value="{$plan['continue_num']}" /></td>
                                        <td><input type="number" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^0-9]/g,'')}else{this.value=this.value.replace(/\D/g,'')}"
                                                   onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^0-9]/g,'0')}else{this.value=this.value.replace(/\D/g,'')}" class="input-text continue_price" name="continue_price[{$plan['id']}]" value="{$plan['continue_price']}" /></td>
                                        <td class="text-c">
                                            <a class="btn-link del-plan"  data-id="{$plan['id']}" href="javascript:;">删除</a>
                                        </td>
                                    </tr>
                                    {/loop}


                                    </tbody>
                                </table>
                                <div class="clearfix">
                                    <a href="javascript:;" id="add-plan" class="btn btn-primary radius size-S mt-3">添加区域</a>
                                </div>
                            </div>
                        </li>
                    </ul>
                    <div class="clear clearfix mt-20">
                        <input type="hidden" value="{$info['id']}" id="templateid" name="templateid">
                        <a href="javascript:;" class="btn btn-primary radius size-L ml-115" id="save_btn">保存</a>
                    </div>
                </div>
                </form>
            </div>

        </td>
    </tr>
</table>

<script>

    $(function () {
        //切换计费方式
        $('input[name=type]').change(function () {
           var val = $(this).val();
           if(val==1)
           {
               var start_text = '首重（g）';
               var continue_text = ' 续重（g）';
           }
           else if(val==2)
           {
               var start_text = '首件（件）';
               var continue_text = '续件（件）';
           }
           else if(val==3)
           {
               var start_text = '首件（立方米）';
               var continue_text = '续件（立方米）';
           }
           $('#type_start').text(start_text);
           $('#type_continue').text(continue_text);
        });
        //添加区域
        $('#add-plan').click(function () {
            var length = $('#planlist tr').length;
            var now_id ='new_'+length++;
            var html = '<tr>' +
                '<td><span></span><br><a class="btn-link edit-plan" data-id="'+now_id+'" href="javascript:;">编辑</a>' +
                '<input class="provincelist"  type="hidden" name="provincelist['+now_id+']">' +
                '<input class="citylist"  type="hidden" name="citylist['+now_id+']"></td>' +
                '<td><input type="number" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,\'\')}else{this.value=this.value.replace(/\\D/g,\'\')}"  \n' +
                '    onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,\'0\')}else{this.value=this.value.replace(/\\D/g,\'\')}" class="input-text start_num" name="start_num['+now_id+']" value="" /></td>' +
                '<td><input type="number" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,\'\')}else{this.value=this.value.replace(/\\D/g,\'\')}"  \n' +
                '    onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,\'0\')}else{this.value=this.value.replace(/\\D/g,\'\')}" class="input-text start_price" name="start_price['+now_id+']" value="" /></td>' +
                '<td><input type="number" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,\'\')}else{this.value=this.value.replace(/\\D/g,\'\')}"  \n' +
                '    onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,\'0\')}else{this.value=this.value.replace(/\\D/g,\'\')}" class="input-text continue_num" name="continue_num['+now_id+']" value="" /></td>' +
                '<td><input type="number" onkeyup="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,\'\')}else{this.value=this.value.replace(/\\D/g,\'\')}"  \n' +
                '    onafterpaste="if(this.value.length==1){this.value=this.value.replace(/[^1-9]/g,\'0\')}else{this.value=this.value.replace(/\\D/g,\'\')}" class="input-text continue_price" name="continue_price['+now_id+']" value="" /></td>' +
                '<td class="text-c"><a class="btn-link del-plan" data-id="'+now_id+'"  href="javascript:;">删除</a></td>' +
                '</tr>';
            $('#planlist').append(html);
        });
        //修改区域
        $('#planlist').on('click','.edit-plan',function () {
            var provincelist = $(this).parent().find('.provincelist').val();
            var citylist = $(this).parent().find('.citylist').val();
            var planid = $(this).data('id');
            ST.Util.showBox("配送区域",SITEURL+'express/dialog_address?provincelist='+provincelist+'&citylist='+citylist+'&planid='+planid,600,'',null,null,document,{loadWindow: window, loadCallback: setAddress});
        });

        //删除
        $('#planlist').on('click','.del-plan',function () {
            var obj = $(this);
            var id = $(obj).data('id');
            ST.Util.confirmBox('提示','确认要删除吗?',function () {
                $.ajax({
                    type:'post',
                    dataType:'json',
                    data:{planid:id},
                    url: SITEURL+"express/ajax_del_plan",
                    success:function (data) {
                        if(data.status)
                        {
                            obj.parent().parent().remove();
                        }
                    }

                })




            });
        });



        //保存
        $('#save_btn').click(function (data) {
            var title = $('input[name=title]').val();
            if(title=='')
            {
                ST.Util.showMsg('请填写配送方式名称',5,1000);
                return false;
            }
            var check = true;
            if(!check)
            {
                ST.Util.showMsg('请完善计费信息',5,1000);
                return false;
            }
            $.ajax({
                type:'post',
                dataType:'json',
                data:$('#st_frm').serialize(),
                url: SITEURL+"express/ajax_save_template",
                success:function (data) {
                    if(data.templateid)
                    {
                        ST.Util.showMsg('保存成功!',4,1000);
                        $('#templateid').val(data.templateid)
                    }
                }

            })

        })


    });
    //设置配送城市
    function setAddress(result,bool)
    {
        var provinceids = result.provinceids;
        var citylist = result.citylist;
        var planid = result.planid;
        $('#planlist .edit-plan[data-id='+planid+']').parent().find('.provincelist').val(provinceids);
        $('#planlist .edit-plan[data-id='+planid+']').parent().find('.citylist').val(citylist);
        $.ajax({
            type:'post',
            dataType:'json',
            data:{citylist:citylist},
            url: SITEURL+"express/ajax_get_address_title",
            success:function (data) {
                $('#planlist .edit-plan[data-id='+planid+']').prev().prev().html(data.cityname)


            }





        });







    }









</script>

</body>
</html>
